﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SignalRDemo.Common.Extensions;

namespace SignalRDemo.Common.Helpers
{
    public class SignalRHelper
    {
        /// <summary>
        /// 个人信息key
        /// </summary>
        private const string _signalrUserKey = "SignalRUserMsg";

        /// <summary>
        /// 保存用户与链接的对应关系
        /// </summary>
        /// <param name="token">用户id，根据token获取到对应的用户id</param>
        /// <param name="connectionId">链接id</param>
        public static bool AddConnectionId(string token, string connectionId)
        {

            var cache = CacheHelper.GetCache(_signalrUserKey);
            if (cache.IsNotNull())
            {
                var models = cache.ToJson().DeserializeJsonToList<SignalRModel>();
                if (models.IsNotNull())
                {
                    models = models.Where(m => m.PassDateTime > DateTime.Now).ToList();//过滤掉过期的
                    var res = models.Any(m => m.token == token && m.ConnectionId == connectionId);
                    if (!res)
                    {
                        var t = new SignalRModel
                        {
                            token = token,
                            ConnectionId = connectionId,
                            PassDateTime = DateTime.Now.AddDays(1)
                        };
                        models.Add(t);
                        CacheHelper.SetCache(_signalrUserKey, models);
                    }
                }
            }
            else
            {
                cache = new List<SignalRModel>
                {
                    new SignalRModel
                    {
                        token = token,
                        ConnectionId = connectionId,
                        PassDateTime = DateTime.Now.AddDays(1)
                    }
                };
                CacheHelper.SetCache(_signalrUserKey, cache);
            }
            return true;
        }

        /// <summary>
        /// 获取用户对应的链接id
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static List<string> GetUserConnectionIds(string token)
        {
            var result = new List<string>();
            var cache = CacheHelper.GetCache(_signalrUserKey);
            if (cache.IsNotNull())
            {
                var models = cache.ToJson().DeserializeJsonToList<SignalRModel>();
                if (models.IsNotNull())
                {
                    models = models.Where(m => m.PassDateTime > DateTime.Now).ToList();//过滤掉过期的数据
                    result = models.Where(m => m.token == token).Select(m => m.ConnectionId).ToList();
                    CacheHelper.SetCache(_signalrUserKey, models);
                }
                    
            }

            return result;
        }

    }


    /// <summary>
    /// SignalR数据实体
    /// </summary>
    public class SignalRModel
    {
        /// <summary>
        /// 用户令牌
        /// </summary>
        public string token { set; get; }

        /// <summary>
        /// 链接标识
        /// </summary>
        public string ConnectionId { set; get; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime PassDateTime { set; get; }
    }
}